﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace CenterDateAchieve.Helper
{
    public  class TreeViewHelper
    {
        private static readonly string ID = "id";
        private static readonly string TEXT = "txt";
        private static readonly string PID = "pid";
        private static readonly string CHECK = "choose";
        private static TreeView tree_;
        public static void BulidTreeView(TreeView treeView, string name, DataSet ds, ContextMenuStrip MenuStrip)
        {

            tree_ = treeView;
            tree_.Nodes.Clear();
            TreeNode root = new TreeNode(name);
            tree_.Nodes.Add(root);
            BulidTreeView(root, ds, MenuStrip);
        }
        /*
          id    txt       pid
          1     name    
          11    name_11    1
          12    xtx        1
          123  424         12
         */
        /// <summary>
        /// 
        /// </summary>
        /// <param name="node"></param>
        /// <param name="ds"></param>
        private static void BulidTreeView(TreeNode node, DataSet ds, ContextMenuStrip MenuStrip)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToString(dr[PID]).Trim() == string.Empty)
                {
                  TreeNode c_node= BulidTreeView(node, dr);
                  if (MenuStrip!=null)
                  c_node.ContextMenuStrip = MenuStrip;
                }
                else
                {
                   TreeNode node1=GetNode(tree_, dr[PID].ToString());
                   if (node1 == null)
                       node1 = node;
                   BulidTreeView(node1, dr);
                }
            }
        }
        private static TreeNode BulidTreeView(TreeNode node, DataRow dr)
        {
            TreeNode tNode = new TreeNode();
            tNode.Checked = Convert.ToBoolean(dr[CHECK]);
            if (tNode.Checked)
                node.Checked = true;
            tNode.Text = Convert.ToString(dr[TEXT]).Trim();
            tNode.Name = Convert.ToString(dr[ID]).Trim();
            node.Nodes.Add(tNode);
            return tNode;
        }
        private static TreeNode GetNode(TreeView treeView, string ID)
        {
            foreach (TreeNode node in treeView.Nodes)
            {
               return GetNode(node, ID);
            }
            return null;
        }
        private static TreeNode GetNode(TreeNode node, string ID)
        {
            foreach (TreeNode treeNode in node.Nodes)
            {
                if (treeNode.Name.Trim() == ID)
                    return treeNode;
                else
                {
                   return GetNode(treeNode, ID);
                }
            }
            return null;
        }
        public static void NodeChecked(TreeNode Node)
        {
            CheckAllChildNodes(Node, Node.Checked);

            //选中父节点
            bool bol = true;
            if (Node.Parent != null)
            {
                for (int i = 0; i < Node.Parent.Nodes.Count; i++)
                {
                    if (!Node.Parent.Nodes[i].Checked)
                        bol = false;
                }
                Node.Parent.Checked = bol;
            }
        }
        //选中子节点
        private static void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    CheckAllChildNodes(node, nodeChecked);
                }
            }
        }
        public static TreeNode GetCheckedNode(TreeNode treeNode)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                if (node.Checked)
                {
                    return node;
                }
            }
            return null;
        }
        public static List<string> GetCheckedNodes(TreeNode treeNode)
        {
            List<string> Nodes = new List<string>();
            foreach (TreeNode node in treeNode.Nodes)
            {
                if (node.Checked)
                {
                    Nodes.Add(node.Name); 
                }
            }
            return Nodes;
        }

        public static void CheckNode(TreeNode node)
        {
            if (node.Parent != null)
            {
                foreach (TreeNode node1 in node.Parent.Nodes)
                {
                    if (node1.Name != node.Name)
                    {
                        node1.Checked = false;
                    }
                    else
                        node1.Checked = true;
                }
            }
        }
    }
}
